package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItem;
import com.itheima.dao.ICheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.ICheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = ICheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements ICheckItemService {

    @Autowired
    private ICheckItemDao checkItemDao;

    /**
     * 添加检查项
     * @param item
     */
    @Override
    public void add(CheckItem item) {
            checkItemDao.add(item);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPageByCondition(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<CheckItem> page = checkItemDao.findPageByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total,rows);
    }

    /**
     * 删除检查项
     * @param id
     */
    @Override
    public void delete(Integer id) {

        // 判断是否添加到检查组
        long count = checkItemDao.findCountByCheckItemId(id);
        // 已添加到检查组
        if (count > 0){
           throw  new RuntimeException();
        }
        // 未添加到检查组
        checkItemDao.deleteById(id);

    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }


    /**
     * 编辑功能
     * @param checkItem
     */
    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<CheckItem> findAll() {

        return checkItemDao.findAll();
    }


}
